//
//  XYTabBarController.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/10.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //中间按钮的点击事件
        let tab=XYTabBar()
        tab.plusButtonBlock={
            printLog("中间按钮的闭包点击事件")
            
            let composeView = XYComposeView()
            
            composeView.show(self)
        }
        
        
        //将tabbar设为自定义的(tabbar是私有只读的,所以需要kvc,运行时来改变)
        setValue(tab, forKey: "tabBar")
        
        
        //添加子控制器
        loadChildContorllers()
        
        
    }
    
    private func loadChildContorllers(){
        loadVCController(XYHomeTableVC(),"tabbar_home","首页")
        loadVCController(XYMessageVC(),"tabbar_message_center","消息")
        loadVCController(XYDiscoverVC(),"tabbar_discover","发现")
        loadVCController(XYProfileVC(),"tabbar_profile","我")
        
    }
    
    private func loadVCController(childVC:UIViewController,_ imageName:String,_ titleName:String){
        //把tabbarItem 修改为自定义的item
        childVC.tabBarItem = XYTabBarItem()
        
        //设置tabbar的图片
        childVC.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        childVC.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置title
        //设置title的字典属性
        childVC.title = titleName
        
        let attr = [NSForegroundColorAttributeName:UIColor.orangeColor()]
        
        childVC.tabBarItem.setTitleTextAttributes(attr, forState: UIControlState.Selected)
        
        
        //包装成nav给tabbar
        let nav = UINavigationController(rootViewController: childVC)
        
        addChildViewController(nav)
        
    }
    
    
    @objc func didPlusButton(){
        print("1")
    }
}
